package com.prime.tutorial.moviestore.dao;


import com.prime.tutorial.moviestore.Constants;
import com.prime.tutorial.moviestore.domain.*;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

@Repository
public class LogicalViewDAOWithJPA extends GenericDAOWithJPA<Object, Long> implements LogicalViewDAO {

    public List<Country> getCountriesWithDevices() {
        Query query = entityManager.createQuery("Select distinct l.country from Device d inner join d.location as l");
        return query.getResultList();
    }

    public List<City> getCitiesWithDevices(Long country_id) {
        Query query = entityManager.createQuery("Select distinct l.city from Device d inner join d.location as l inner join l.country as co where co.id = '"+country_id+"'");
        return query.getResultList();
    }

    public List<Device> getCoreDevices(Long city_id) {
        Query query = entityManager.createQuery("Select distinct d from Device d where d.location.city.id ='"+city_id+"' and not exists " +
                "(select dtd.deviceport_to.device from DeviceToDevice dtd where dtd.deviceport_from.device = d)" +
                "and not d.state.type='"+ Constants.STATE_ON_STORAGE +"'");
//                "Select distinct d from Device d inner join d.location as l inner join l.city as c where c.id = '"+city_id+"'");
        return query.getResultList();
    }

    public List<Device> getChildDevices(Long device_id) {
        Query query = entityManager.createQuery("Select distinct dpf.device from DeviceToDevice dtd inner join dtd.deviceport_to as dpt inner join dtd.deviceport_from as dpf where dpt.device.id = '"+device_id+"'");
//        query.setParameter(1, device_id.toString());
        return query.getResultList();
    }

    public List<DeviceToDevice> getChildConnections(Long device_id) {
        Query query = entityManager.createQuery("Select distinct dtd from DeviceToDevice dtd inner join dtd.deviceport_to as dpt where dpt.device.id = '"+device_id+"'");
//        query.setParameter(1, device_id.toString());
        return query.getResultList();
    }

    public List<Port> getPorts(Long device_id) {
        Query query = entityManager.createQuery("Select distinct p from DevicePort dp inner join dp.port as p where dp.device.id = '"+device_id+"'");
//        query.setParameter(1, device_id.toString());
        return query.getResultList();
    }

    public List<Device> getDevicesByPort(Long portType_id) {
        Query query = entityManager.createQuery("Select distinct dp.device from DevicePort dp inner join dp.port as p inner join p.port_type as pt where pt.id = '"+portType_id+"'");
//        query.setParameter(1, portType_id.toString());
        return query.getResultList();
    }

    public List<Device> getDevicesByType(String deviceType) {
        Query query = entityManager.createQuery("Select distinct d from Device d inner join d.model as m inner join m.modelType as mt where mt.type='"+deviceType+"'");
//        query.setParameter(1, portType_id.toString());
        return query.getResultList();
    }

    public List<Model> getModelsByType(String deviceType) {
        Query query = entityManager.createQuery("Select distinct m from Model m inner join m.modelType as mt where mt.type ='"+deviceType+"'");
        return query.getResultList();
    }

    public List<Device> getDeviceByPorts(List<Integer> portTypeIDs) {
        Query query = entityManager.createQuery("Select distinct dp.device from DevicePort dp inner join dp.port as p inner join p.port_type as pt where pt.id = ?1 ");
        query.setParameter(1, portTypeIDs);
        return query.getResultList();
    }

    public List<ModelType> getModelTypeByDeviceId(Integer deviceId) {
        Query query = entityManager.createQuery("Select distinct mt from Device d inner join dp.model as m inner join m.modelType as mt where d.id = '"+deviceId+"'");
        return query.getResultList();
    }

    public List<Model> getModelsByType(String modelType, Integer deviceId) {
        Query query = entityManager.createQuery("Select distinct m from Model m inner join m.modelType as mt where mt.type ='"+modelType+"'");
        return query.getResultList();
    }


}
